package com.cuit.controller;

import com.alibaba.fastjson.JSON;
import com.cuit.mapper.Book.BookTypeMapper;
import com.cuit.mapper.BookMapper;
import com.cuit.pojo.Book;
import com.cuit.pojo.BookKeep;
import com.cuit.result.R;
import com.cuit.service.BookKeepService;
import com.cuit.service.BookService;
import com.cuit.service.FileService;
import com.cuit.utils.FastDFSClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@CrossOrigin
@RequestMapping("/bookput")
public class BookPutController {


    @Autowired
    private BookService bookService;

    @Autowired
    private BookKeepService bookKeepService;

    @Autowired
    private FastDFSClientUtil fastDFSClientUtil;

    @Autowired
    private FileService fileService;

    @PostMapping(value="/insert")
    public R insert(@RequestParam("book") String bookStr, @RequestParam("file") MultipartFile file,@RequestParam("id") Integer uid) throws IOException {
//    public R insert(@RequestParam("book") String bookStr, @RequestParam("file") MultipartFile file) throws IOException {
        Book book = JSON.parseObject(bookStr, Book.class);
        System.out.println(book);
          String fileUrl=fileService.getImgPathSQL(file);
        System.out.println(fileUrl);
//        Book book=new Book();
//        book.setBookName(bookName);
//        book.setBookPrice(bookPrice);
//        book.setBookTypeName(bookTypeName);
//        book.setBookDesc(bookDesc);
//        System.out.println(book.getBookImg());
         book.setUserid(uid);
         book.setBookimg(fileUrl);
         book.setSellnum(32);
//        book.setBookImg(fastDFSClientUtil.uploadFile(file));
        boolean save=bookService.save(book);
        if(save){
            System.out.println(save);
            return R.ok();
        }else{
            return R.error();
        }
    }

    @PostMapping(value="/keep")
    public R keep(@RequestParam("book") String bookStr, @RequestParam("file") MultipartFile file,@RequestParam("id") Integer uid) throws IOException {
        BookKeep bookKeep=JSON.parseObject(bookStr,BookKeep.class);
//        System.out.println(bookKeep);
        String fileUrl=fileService.getImgPathSQL(file);
//        System.out.println(fileUrl);
        bookKeep.setUserId(uid);
        bookKeep.setBookImg(fileUrl);
        bookKeep.setSellNum("32");
        boolean save=bookKeepService.save(bookKeep);
        if(save){
            return R.ok();
        }else{
            return R.error();
        }
    }

}
